﻿using RepositoryDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace ForumBL
{
    public class UserBL
    {
        IUserRepository userContext;

        public UserBL()
        {
            userContext = IOC.Factory.GetRepository().GetUserRepository();
        }

        public bool IsUsernameBusy(string username)
        {
            ForumDomainModels.User user = new ForumDomainModels.User();
            user.Username = username;
            int count = userContext.GetUserCountByUsername(user);
            if (count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }

        public bool IsValidUser(ForumDomainModels.User user)
        {
            Hash hasObject = new Hash();
            user.Password = hasObject.Sha256(user.Password);
            int count = userContext.GetUserCountByUsernameAndPassword(user);
            if (count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public ForumDomainModels.User GetUserByUsernameAndPassword(ForumDomainModels.User user)
        {   
            Hash hashObject = new Hash();
            user.Password = hashObject.Sha256(user.Password);
            ForumDomainModels.User retrievedUser = userContext.GetUserByUsernameAndPassword(user);
            return retrievedUser;
        }

        public void InsertUser(ForumDomainModels.User user)
        {
            Hash hashObject = new Hash();
            user.Password = hashObject.Sha256(user.Password);
            userContext.InsertUser(user);
        }

        public ForumDomainModels.User GetUserById(ForumDomainModels.User creatorPost)
        {
            ForumDomainModels.User user = userContext.GetUserById(creatorPost);
            return user;
        }
    }
}
